'use strict';
var Generator = require('yeoman-generator');
var mkdirp = require('mkdirp');
var yosay = require('yosay');
var chalk = require('chalk');
var _ = require('lodash');
var extend = require('deep-extend');
var path = require('path');
module.exports = class extends Generator {
  constructor(args, opts) {
    // Calling the super constructor is important so our generator is correctly set up
    super(args, opts);
    this.option('wap', {
      type: Boolean,
      description: '移动端项目，默认false'
    });
    this.option('full', {
      type: Boolean,
      description: '加载整屏js，默认false'
    });
    this.option('help', {
      type: Boolean,
      alias: 'h',
      description: 'help'
    });
    this.option('skip-cache', {
      type: Boolean,
      hide: true
    });
    this.option('skip-install', {
      type: Boolean,
      description: '跳过安装依赖'
    });
  }

  initializing() {
    this.props = {};
    var message = chalk.bgBlack.bold('\nWelcome to vvey-web\n');
    this.log(yosay(message));
  };
  prompting() {
    const prompts = [{
      type: 'input',
      name: 'name',
      message: '项目名称-文件名:'
    },
    {
      type: 'input',
      name: 'describe',
      message: '项目描述:'
    }
      ,
    {
      type: 'input',
      name: 'author',
      message: '作者:'
    }
    ];
    const done = this.async();
    this.prompt(prompts).then((answers) => {
      this.props = answers;
      var done = this.async();
      mkdirp(this.props.name);
      this.destinationRoot(this.props.name);
      this.config.save();
      done();
    });
  };
  writing() {
    // config
    var pkg = this.fs.readJSON(this.templatePath('config/package.json'), {});
    pkg.keywords = ['vvey-web', 'generator', this.props.name];
    pkg.name = this.props.name;
    pkg.description = this.props.describe;
    pkg.author = this.props.author;
    this.fs.writeJSON(this.destinationPath('package.json'), pkg);
    this.fs.copyTpl(
      this.templatePath('config/.babelrc'),
      this.destinationPath('.babelrc')
    );
    this.fs.copyTpl(
      this.templatePath('config/postcss.config.js'),
      this.destinationPath('postcss.config.js')
    );
    this.fs.copyTpl(
      this.templatePath('config/gitignore.txt'),
      this.destinationPath('.gitignore')
    );
    var readmeTmpl = _.template(this.fs.read(this.templatePath('config/README.md')));
    this.fs.write(this.destinationPath('README.md'), readmeTmpl({
      name: this.props.name,
      describe: this.props.describe
    }));
    // config --end

    // js css
    mkdirp('src/components');
    mkdirp('src/css');
    mkdirp('src/lib');
    mkdirp('src/images');
    // mkdirp('config');
    const temp_css = this.options.wap ? 'css/_wap_common.css' : 'css/_pc_common.css';
    const temp_js = this.options.wap ? "lib/_wap_common.js" : "lib/_pc_common.js";
    this.fs.copyTpl(
      this.templatePath(temp_css),
      this.destinationPath('src/css/common.css')
    );
    this.fs.copyTpl(
      this.templatePath(temp_js),
      this.destinationPath('src/components/common.js')
    );
    if (this.options.full) {
      var readfull = this.fs.read(this.templatePath('lib/_full.screen.js'));
      this.fs.append(
        this.destinationPath('src/components/common.js'),
        readfull
      );
    }
    this.fs.append(
      this.destinationPath('src/components/common.js'),
      'if(typeof exports ==="object"){module.exports=jQuery;}'
    );
    if (this.options.wap) {
      this.fs.copyTpl(
        this.templatePath('lib/_adapter.js'),
        this.destinationPath('src/components/adapter.js')
      );
      this.fs.copyTpl(
        this.templatePath('lib/jquery-3.0.0.min.js'),
        this.destinationPath('src/lib/jquery.min.js')
      );
    } else {
      this.fs.copyTpl(
        this.templatePath('lib/jquery-1.11.1.min.js'),
        this.destinationPath('src/lib/jquery.min.js')
      );
    };
    // js css --end
    this.fs.copyTpl(
      this.templatePath('index.js'),
      this.destinationPath('src/index.js')
    );
    this.fs.copyTpl(
      this.templatePath('index.html'),
      this.destinationPath('src/index.html')
    );
    // webpack 
    this.fs.copyTpl(
      this.templatePath('webpack/helper.js'),
      this.destinationPath('config/helper.js')
    );
    this.fs.copyTpl(
      this.templatePath('webpack/webpack.common.js'),
      this.destinationPath('config/webpack.common.js')
    );
    this.fs.copyTpl(
      this.templatePath('webpack/webpack.dev.js'),
      this.destinationPath('config/webpack.dev.js')
    );
    this.fs.copyTpl(
      this.templatePath('webpack/webpack.pro.js'),
      this.destinationPath('config/webpack.pro.js')
    );
    this.fs.copyTpl(
      this.templatePath('webpack/webpack.config.js'),
      this.destinationPath('webpack.config.js')
    );
  }
  install() {
    this.log('安装依赖...');
    this.npmInstall()
  };
  end() {
    this.log(yosay('创建完成'));
  }
};
